import asyncio
import json
import time

from pymongo.errors import DuplicateKeyError

from re_common.baselibrary.utils.basedir import BaseDir
from re_common.baselibrary.utils.basefile import BaseFile
from re_common.baselibrary.utils.basemotor import BaseMotor

bs = BaseMotor()
bs.AsyncIOMotorClient("mongodb://192.168.31.168:27017","collection")
bs.get_col("wf_journal_jsons")


async def insert_file(filename, bs):
    jsonText = BaseFile.single_read_file(filename)

    dictAll = json.loads(jsonText)
    for dicts in dictAll['value']:
        dicts["_id"] = dicts["Id"]
        try:
            await bs.insert_one(dicts)
        except DuplicateKeyError:
            pass


start_time = time.time()
lists = []
for file in BaseDir.get_dir_all_files(r"D:\download\wanfang_qk\download\journal\json\a_z"):
    lists.append(insert_file(file, bs))

asyncio.get_event_loop().run_until_complete(asyncio.wait(lists))
print(time.time() - start_time)
